package com.erpanderp.wam.asset.view;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;

import com.erpanderp.wam.asset.model.entity.AssetRequest;
import com.erpanderp.wam.asset.model.service.AssetRequestDAO;
import com.erpanderp.wam.usm.model.entity.user.WAMUser;
import com.erpanderp.wam.util.CommonLookup;

@ViewScoped
@ManagedBean
public class AssetRequestsBean implements Serializable{


	private static final long serialVersionUID = 1L;
	private HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
	ServletContext context = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();
	private WAMUser wamUser = (WAMUser) session.getAttribute(CommonLookup.SESSION_ATTRIBUTE_WAMUSER);
	
	private List<AssetRequest> assetRequestList = new ArrayList<AssetRequest>();
	
	private AssetRequestDAO assetRequestDAO =new AssetRequestDAO();
	
	private int SNO=1;

	public AssetRequestsBean() {
		
		assetRequestList = assetRequestDAO.getAllAssetRequestsByEmployeeId(wamUser.getEmployee().getEmployeeId());
		for(AssetRequest assetRequest: assetRequestList){
			assetRequest.setSno(SNO);
			SNO++;
		}
	}
	
	 //View EXPENSE FORM 
	 public String viewAssetRequestAction() {
		 
	   try{
			  Map<String,String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
			  
			  String assetCode = params.get("assetCode");
			  System.out.println("=====travelCode======"+assetCode);
			  
			  FacesContext.getCurrentInstance().getExternalContext().getRequestMap().put("assetCode", assetCode);		  
			  FacesContext.getCurrentInstance().getExternalContext().redirect("/wam/asset/viewassetrequests.html?assetCode="+assetCode+"&employeeCode="+wamUser.getEmployee().getEmployeeCode());
			  
		  }catch (Exception e) {
			  System.out.println("INSIDE CATCH EXCPTION:::::::::"+e);
			  e.printStackTrace();
		 }
		 return "viewassetrequests";
	}

	//GENERATE SETTERS AND GETTERS
	public List<AssetRequest> getAssetRequestList() {
		return assetRequestList;
	}

	public void setAssetRequestList(List<AssetRequest> assetRequestList) {
		this.assetRequestList = assetRequestList;
	}

	public WAMUser getWamUser() {
		return wamUser;
	}

	public void setWamUser(WAMUser wamUser) {
		this.wamUser = wamUser;
	}

	public AssetRequestDAO getAssetRequestDAO() {
		return assetRequestDAO;
	}

	public void setAssetRequestDAO(AssetRequestDAO assetRequestDAO) {
		this.assetRequestDAO = assetRequestDAO;
	}

}
